# coding=utf-8
import pika

from rabbitmqclass.Rabbitmqpool import Rabbitmqpool

if __name__ == '__main__':
    rabbitmq_host = "10.86.3.178"
    rabbitmq_port = "5672"
    rabbitmq_user = "admin"
    rabbitmq_password = "msdi@rabbit"
    rabbitmq_virtual_host = ""
    credentials = pika.PlainCredentials(rabbitmq_user, rabbitmq_password)
    Pool = Rabbitmqpool(3, 20)
    cert = Pool.get_certtemplate()
    cert['rabbitmq_host'] = rabbitmq_host
    cert['rabbitmq_virtual_host'] = rabbitmq_virtual_host
    cert['rabbitmq_user'] = rabbitmq_user
    cert['rabbitmq_password'] = rabbitmq_password
    cert['rabbitmq_port'] = rabbitmq_port
    Pool.addcert(cert)
    # 队列名
    queueName='GJLZHFJYXT'
    data = {"id": "2DD62BC85B7D49B0AD2B7DD16A32809A", "comFax": "0871-65660628", "account": "", "comAddr": "",
            "comCode": "91530000676581175P", "comName": "云南滇能智慧能源有限公司", "comType": "01",
            "imgName": "companyCard/10c64a14e92c494798d68e5dce72ecb7.jpg", "addrCity": "昆明市", "codeType": "01",
            "comGroup": "", "comPhone": "0871-65660617", "parentId": "00000000000000000000000000000001",
            "comNatuke": "其他有限责任公司", "isDefault": 0, "isDeleted": "0", "addrCounty": "五华区", "auditState": 3,
            "createTime": "2023-08-07 00:00:00.000000", "createUser": "0", "dataSource": "tongyirenzheng",
            "updateTime": "2024-07-16 15:24:53.000000", "updateUser": "-1",
            "imgAuthName": "other/20240716_151857960.png", "addrProvince": "云南省", "codeTypeName": "营业执照",
            "fullParentId": "00000000000000000000000000000001,00000000000000000000000000000001"}

    c, cname = Pool.get_channel()
    try:

        c.basic_publish(exchange='',
                        routing_key=queueName,
                        body=str(data),
                        )
        Pool.return_channel(c, cname)
    except Exception as e:
        print("发送错误：", e)  # 链接过期
        Pool.delconnection(cname)  # channel过期时，删除此链接和此链接下的所有channel
        c, cname = Pool.create_channel()  # 创建一个新的链接和channel
        c.basic_publish(exchange='',
                        routing_key=queueName,
                        body=str(data),
                        )
        Pool.return_channel(c, cname)
    print('发送成功')